#include <string.h>
#include "sipsiparser.h"
#include "buildersectioncontrol.h"
#include "pluginbat.h"
#include "pluginnit.h"
#include "builderplugin.h"
#include "plugindvbfun.h"
#include "dtmbfreqtable.h"

typedef enum
{
	PLUGIN_AUTO_STATUS_IDLE,
	PLUGIN_AUTO_STATUS_WAIT_LOCK,
	PLUGIN_AUTO_STATUS_RECV_SIPSI,
	PLUGIN_AUTO_STATUS_RECV_BAT,
	PLUGIN_AUTO_STATUS_RECV_NIT,
}PLUGIN_AUTO_STATUS_T;

typedef struct
{
	PLUGIN_AUTO_STATUS_T			m_eStatus;
	//bat
	unsigned int						m_u32BatRecved;
	builder_bat_control_t				m_stBatRecvCtrl;
	
	builder_tuner_t					m_stCurTuner;
	unsigned int						m_u32Process;
	builder_section_control_t			m_stSecCtrl;
	unsigned int						m_u32DataOK;
	builder_tuner_t					m_stDataOkTunerInfo;
	unsigned int						m_u32TimeOut;

	//nit
	unsigned int						m_u32NitFreqCnt;
	builder_tuner_t*					m_pstTunerList;
	builder_nit_control_t				m_stNitRecvCtrl;
	unsigned int						m_u32Index;
	unsigned int						m_u32NitRecved;
}builder_auto_search_t;

builder_auto_search_t pstAutoCtrl = {PLUGIN_AUTO_STATUS_IDLE};

static builder_tuner_t astShanxiDTMBFreqTable[] = 
{
	 {794000, 6875, 64, 8, BUILDER_TUNER_DVBT}, 
	 {802000, 6875, 64, 8, BUILDER_TUNER_DVBT},
	 {754000, 6875, 64, 8, BUILDER_TUNER_DVBT},
};

int PluginAutoSearchReset()
{
	int s32Ret = -1;
	
	PluginDvbLogcat("PluginAutoSearchReset() - entry\r\n");	
	
	pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;
	pstAutoCtrl.m_u32DataOK = 0;
	pstAutoCtrl.m_u32Process = 0;
	pstAutoCtrl.m_u32TimeOut = 0;
	
	pstAutoCtrl.m_u32BatRecved = 0;
	PluginBatStop(&pstAutoCtrl.m_stBatRecvCtrl);

	pstAutoCtrl.m_u32NitRecved = 0;
	PluginNitStop(&pstAutoCtrl.m_stNitRecvCtrl);

	s32Ret = PluginSectionDataStop(&pstAutoCtrl.m_stSecCtrl);

	if(pstAutoCtrl.m_pstTunerList &&  pstAutoCtrl.m_pstTunerList != astShanxiDTMBFreqTable)
		PluginDvbMemFree(pstAutoCtrl.m_pstTunerList);
	pstAutoCtrl.m_pstTunerList = NULL;
	
	PluginDvbLogcat("PluginAutoSearchReset() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;		
}

int PluginAutoSearchStart()
{
	int s32Ret = -1;
	unsigned int u32FreqCnt;
	unsigned int i;
	
	PluginDvbLogcat("PluginAutoSearchStart() - entry\r\n");
	u32FreqCnt = 0;
	pstAutoCtrl.m_u32BatRecved = 0;
	pstAutoCtrl.m_pstTunerList = astShanxiDTMBFreqTable;

	pstAutoCtrl.m_u32NitFreqCnt = sizeof(astShanxiDTMBFreqTable)/sizeof(astShanxiDTMBFreqTable[0]);
	pstAutoCtrl.m_u32Index = 0;
	memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
	s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
	pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;
	pstAutoCtrl.m_u32NitRecved = 0;
	pstAutoCtrl.m_u32TimeOut = 2;
	PluginDvbLogcat("PluginAutoSearchStart() - return, s32Ret = %d\r\n", s32Ret);
	
	return s32Ret;		
}

int PluginAutoSearchTick()
{
	int s32Ret = -1;
	
	PluginDvbLogcat("PluginAutoSearchTick() - entry\r\n");
	if(pstAutoCtrl.m_eStatus == PLUGIN_AUTO_STATUS_RECV_NIT)
	{
		s32Ret = PluginNitTick(&pstAutoCtrl.m_stNitRecvCtrl);
		if(s32Ret != 0)
		{
			s32Ret = 0;
			PluginNitStop(&pstAutoCtrl.m_stNitRecvCtrl);
			
			//next freq
			pstAutoCtrl.m_u32Index++;
			pstAutoCtrl.m_u32Process = (pstAutoCtrl.m_u32Index)*10/pstAutoCtrl.m_u32NitFreqCnt;
			if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
			{
				PluginNitReset(&pstAutoCtrl.m_stNitRecvCtrl);
				memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
				PluginDvbLogcat("PluginAutoSearchTick() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
				s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
				pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
			}
			else
			{
				PluginDvbLogcat("PluginAutoSearchTick() - all freq index end\r\n");
				s32Ret = 0;
				pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
				pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;	
			}
		}
	}
	else if(pstAutoCtrl.m_eStatus == PLUGIN_AUTO_STATUS_RECV_SIPSI)
	{
		s32Ret = PluginSectionDataTick(&pstAutoCtrl.m_stSecCtrl);
		if(s32Ret != 0)
		{
			if (1 == s32Ret)
			{
				pstAutoCtrl.m_u32DataOK = 1;
				memcpy(&pstAutoCtrl.m_stDataOkTunerInfo, &pstAutoCtrl.m_stCurTuner, sizeof(builder_tuner_t));
				s32Ret = 0;
			}
			else 
			{
				pstAutoCtrl.m_u32DataOK = 0;
				s32Ret = PluginSectionDataStop(&pstAutoCtrl.m_stSecCtrl);
				if(s32Ret != 0)
					PluginDvbLogcat("PluginAutoSearchTick() -PluginSectionDataStop error\r\n");
			}
			
			pstAutoCtrl.m_u32Index++;
			pstAutoCtrl.m_u32Process = 10 + pstAutoCtrl.m_u32Index*90/pstAutoCtrl.m_u32NitFreqCnt;
			if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
			{
				memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
				PluginDvbLogcat("PluginAutoSearchTick() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
				s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
				pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
			}
			else
			{
				PluginDvbLogcat("PluginAutoSearchTick() - all freq index end\r\n");
				pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
			}
		}
	}
	else if(pstAutoCtrl.m_eStatus == PLUGIN_AUTO_STATUS_RECV_BAT)
	{
		s32Ret = PluginBatTick(&pstAutoCtrl.m_stBatRecvCtrl);
		if(s32Ret != 0)
		{
			pstAutoCtrl.m_u32DataOK = 1;//recv pat pmt end then recv bat
			s32Ret = 0;
			PluginBatStop(&pstAutoCtrl.m_stBatRecvCtrl);
			pstAutoCtrl.m_u32BatRecved = 0;

			//next freq
			pstAutoCtrl.m_u32Index++;
			pstAutoCtrl.m_u32Process = 10 + pstAutoCtrl.m_u32Index*90/pstAutoCtrl.m_u32NitFreqCnt;
			if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
			{
				memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
				PluginDvbLogcat("PluginAutoSearchTick() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
				s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
				pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
			}
			else
			{
				PluginDvbLogcat("PluginAutoSearchTick() - all freq index end\r\n");
				pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
			}
		}
	}

	PluginDvbLogcat("PluginAutoSearchTick() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;		
}

int PluginAutoSearchLockFinish(builder_tuner_t* pstTunerInfo, unsigned int u32Lock)
{
	int s32Ret = -1;
	
	PluginDvbLogcat("PluginAutoSearchLockFinish() - entry, pstTunerInfo = %p, u32Lock = %d\r\n", pstTunerInfo, u32Lock);
	if(pstTunerInfo && pstAutoCtrl.m_eStatus==PLUGIN_AUTO_STATUS_WAIT_LOCK)
	{
		if(pstAutoCtrl.m_u32NitRecved == 0)
		{
			if(u32Lock == 1)//receive nit
			{
				PluginNitReset(&pstAutoCtrl.m_stNitRecvCtrl);
				s32Ret = PluginNitStart(&pstAutoCtrl.m_stNitRecvCtrl);
				pstAutoCtrl.m_u32Process = (pstAutoCtrl.m_u32Index+1)*10/pstAutoCtrl.m_u32NitFreqCnt;
				pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_RECV_NIT;
				memcpy(&pstAutoCtrl.m_stDataOkTunerInfo, &pstAutoCtrl.m_stCurTuner, sizeof(pstAutoCtrl.m_stDataOkTunerInfo));
			}
			else//next freq
			{
				pstAutoCtrl.m_u32DataOK = 0;
				pstAutoCtrl.m_u32Index++;
				pstAutoCtrl.m_u32Process = pstAutoCtrl.m_u32Index*10/pstAutoCtrl.m_u32NitFreqCnt;
				
				if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
				{
					PluginNitReset(&pstAutoCtrl.m_stNitRecvCtrl);
					memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
					PluginDvbLogcat("PluginAutoSearchLockFinish() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
					s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
				}
				else
				{
					PluginDvbLogcat("PluginAutoSearchLockFinish() - all freq index end\r\n" );
					s32Ret = 0;
					pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;	
				}
			}
		}
		else
		{
			if(u32Lock == 1)
			{
				builder_section_control_cfg_t stCfg;
				stCfg.m_eSecRecvCfg = BUILDER_SECTION_DATA_NOSDT_CONTINUE;
				stCfg.m_u16PSITimeoutRatio = 9;
				stCfg.m_u16SITimeoutRatio = 2;

				PluginSectionDataSetConfig(&pstAutoCtrl.m_stSecCtrl, &stCfg);
				s32Ret = PluginSectionDataReset(&pstAutoCtrl.m_stSecCtrl);
				if(s32Ret == 0)
				{
					pstAutoCtrl.m_u32TimeOut = 4;
					s32Ret = PluginSectionDataStart(&pstAutoCtrl.m_stSecCtrl);
					pstAutoCtrl.m_u32Process = 10 + pstAutoCtrl.m_u32Index*90/pstAutoCtrl.m_u32NitFreqCnt;
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_RECV_SIPSI;
					memcpy(&pstAutoCtrl.m_stDataOkTunerInfo, &pstAutoCtrl.m_stCurTuner, sizeof(pstAutoCtrl.m_stDataOkTunerInfo));
				}
				else
				{
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;
					PluginDvbLogcat("PluginAutoSearchLockFinish() - start recv sdt error\r\n");
				}
			}
			else
			{
				pstAutoCtrl.m_u32DataOK = 0;
				pstAutoCtrl.m_u32Index++;
				pstAutoCtrl.m_u32Process = 10 + pstAutoCtrl.m_u32Index*90/pstAutoCtrl.m_u32NitFreqCnt;
				
				if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
				{
					PluginSectionDataReset(&pstAutoCtrl.m_stSecCtrl);
					memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
					PluginDvbLogcat("PluginAutoSearchLockFinish() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
					s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
				}
				else
				{
					PluginDvbLogcat("PluginAutoSearchLockFinish() - all freq index end\r\n");
					s32Ret = 0;
					pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;	
				}
			}
		}
	}
	else
	{
		PluginDvbLogcat("PluginAutoSearchLockFinish() - receive error message\r\n");
		s32Ret = -1;
	}
	PluginDvbLogcat("PluginAutoSearchLockFinish() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;
}

int PluginAutoSearchHandleDataRecved(unsigned char* pu8Data, unsigned int hFilter)
{
	int s32Ret = -1;
	unsigned int u32Process;

	PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - entry, pu8Data = %p, hFilter = %x\r\n", pu8Data, hFilter);
	if(pu8Data && pstAutoCtrl.m_eStatus==PLUGIN_AUTO_STATUS_RECV_NIT)
	{
		s32Ret = PluginNitRecv(&pstAutoCtrl.m_stNitRecvCtrl, hFilter, pu8Data);
		if(s32Ret == 0)
		{
			PluginNitGetProcess(&pstAutoCtrl.m_stNitRecvCtrl, &u32Process);
			if(u32Process == 100)
			{
				pstAutoCtrl.m_u32DataOK = 1;
				pstAutoCtrl.m_u32NitRecved = 1;
				memcpy(&pstAutoCtrl.m_stDataOkTunerInfo, &pstAutoCtrl.m_stCurTuner, sizeof(builder_tuner_t));	
				//nit loop
				PluginNitGetTunerList(&pstAutoCtrl.m_stNitRecvCtrl, &pstAutoCtrl.m_pstTunerList, &pstAutoCtrl.m_u32NitFreqCnt);
				pstAutoCtrl.m_u32Index = 0;
				if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
				{
					memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
					PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
					s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;
					pstAutoCtrl.m_u32Process = 10;
				}
				else
				{
					s32Ret = 0;
					pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;	
				}
			}
			else
			{
				pstAutoCtrl.m_u32Process = pstAutoCtrl.m_u32Index*10/pstAutoCtrl.m_u32NitFreqCnt + u32Process*10/pstAutoCtrl.m_u32NitFreqCnt/100;
			}
		}
	}
	else if(pu8Data && pstAutoCtrl.m_eStatus==PLUGIN_AUTO_STATUS_RECV_SIPSI)
	{
		s32Ret = PluginSectionDataRecv(&pstAutoCtrl.m_stSecCtrl, hFilter, pu8Data);
		if(s32Ret == 0)
		{
			s32Ret = PluginSectionDataGetProcess(&pstAutoCtrl.m_stSecCtrl, &u32Process);
			if(s32Ret == 0)
			{
				pstAutoCtrl.m_u32Process = 10 + pstAutoCtrl.m_u32Index*90/pstAutoCtrl.m_u32NitFreqCnt;
				pstAutoCtrl.m_u32Process += u32Process*90/pstAutoCtrl.m_u32NitFreqCnt/100;

				if(u32Process == 100 && pstAutoCtrl.m_u32BatRecved==0)
				{
					PluginBatReset(&pstAutoCtrl.m_stBatRecvCtrl);
					s32Ret = PluginBatStart(&pstAutoCtrl.m_stBatRecvCtrl);
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_RECV_BAT;
					pstAutoCtrl.m_u32TimeOut = 0;
				}
				else if(u32Process == 100)
				{
					pstAutoCtrl.m_u32DataOK = 1;
					pstAutoCtrl.m_u32Index++;
					memcpy(&pstAutoCtrl.m_stDataOkTunerInfo, &pstAutoCtrl.m_stCurTuner, sizeof(builder_tuner_t));
					if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
					{
						memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
						PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
						s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
						pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
					}
					else
					{
						PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - all freq index end\r\n");
						pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
					}
				}
			}
			else
			{
				PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - get section control process not OK\r\n");
			}
		}
		else
		{
			PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - send to section control not OK\r\n");
			s32Ret = 0;
		}
	}
	else if(pu8Data && pstAutoCtrl.m_eStatus==PLUGIN_AUTO_STATUS_RECV_BAT)
	{
		s32Ret = PluginBatRecv(&pstAutoCtrl.m_stBatRecvCtrl, hFilter, pu8Data);
		if(s32Ret == 0)
		{
			PluginBatGetProcess(&pstAutoCtrl.m_stBatRecvCtrl, &u32Process);
			if(u32Process == 100)
			{
				pstAutoCtrl.m_u32DataOK = 1;
				pstAutoCtrl.m_u32Index++;
				pstAutoCtrl.m_u32Process = 10 + pstAutoCtrl.m_u32Index*90/pstAutoCtrl.m_u32NitFreqCnt;
				
				memcpy(&pstAutoCtrl.m_stDataOkTunerInfo, &pstAutoCtrl.m_stCurTuner, sizeof(builder_tuner_t));
				if(pstAutoCtrl.m_u32Index < pstAutoCtrl.m_u32NitFreqCnt)
				{
					memcpy(&pstAutoCtrl.m_stCurTuner, &pstAutoCtrl.m_pstTunerList[pstAutoCtrl.m_u32Index], sizeof(builder_tuner_t));
					PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - start next freq stBuilderCtrl.m_stCurTuner.m_u32Freq = %d\r\n", pstAutoCtrl.m_stCurTuner.m_u32Freq);
					s32Ret = PluginDvbTunerLock(&pstAutoCtrl.m_stCurTuner);
					pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_WAIT_LOCK;	
				}
				else
				{
					PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - all freq index end\r\n");
					pstAutoCtrl.m_u32Process = SCAN_ALL_PROCESS;
				}
			}
		}
	}

	PluginDvbLogcat("PluginAutoSearchHandleDataRecved() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;
}


int PluginAutoGetSearchData(builder_ts_t* pstTs, builder_service_t* pstServices, unsigned int* pu32ServiceCnt, builder_section_t** ppstBat, builder_section_t** ppstNit)
{
	int s32Ret = -1;
	builder_ts_t stTs;
	builder_service_t astServices[BUILDER_MAX_PROG_PER_TS];
	unsigned int u32ServiceCnt = 0;
	
	PluginDvbLogcat("PluginAutoGetSearchData() - entry, pstTs = %p, pstServices = %p, pu32ServiceCnt = %p, pstBat = %p, pstNit = %p\r\n", pstTs, pstServices, pu32ServiceCnt, ppstBat, ppstNit);
	if(pstAutoCtrl.m_u32DataOK == 1)
	{
		memset(&stTs, 0, sizeof(stTs));
		s32Ret = PluginSectionDataGetSections(&pstAutoCtrl.m_stSecCtrl, &stTs.m_stSiPsi);
		if(s32Ret == 0)
		{
			if(stTs.m_stSiPsi.m_pstPat && stTs.m_stSiPsi.m_pstPmt)
			{
				memcpy(&stTs.m_stTuner, &pstAutoCtrl.m_stDataOkTunerInfo, sizeof(stTs.m_stTuner));
				s32Ret = PluginParseTs(&stTs, astServices, &u32ServiceCnt);
				if(s32Ret == 0)
				{
					memcpy(pstTs, &stTs, sizeof(stTs));
					*pu32ServiceCnt = u32ServiceCnt;
					memcpy(pstServices, astServices, u32ServiceCnt*sizeof(builder_service_t));
				}
				else
					PluginDvbLogcat("PluginAutoGetSearchData() - PluginParseTs error\r\n");
			}
		}
		
		*ppstBat = NULL;
		PluginBatGetSections(&pstAutoCtrl.m_stBatRecvCtrl, ppstBat);

		*ppstNit= NULL;
		PluginNitGetSections(&pstAutoCtrl.m_stNitRecvCtrl, ppstNit);
		
		pstAutoCtrl.m_u32DataOK = 0;
	}
	PluginDvbLogcat("PluginAutoGetSearchData() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;	
}

int PluginAutoGetProcess(unsigned int* pu32DataOk, unsigned int* pu32Process)
{
	int s32Ret = -1;
	
	PluginDvbLogcat("PluginAutoGetProcess() - entry, pu32DataOk = %p, pu32Process = %p\r\n", pu32DataOk, pu32Process);
	if(pu32DataOk && pu32Process)
	{
		*pu32DataOk = pstAutoCtrl.m_u32DataOK;
		*pu32Process = pstAutoCtrl.m_u32Process;
		if(*pu32Process == SCAN_ALL_PROCESS)
			pstAutoCtrl.m_eStatus = PLUGIN_AUTO_STATUS_IDLE;
		s32Ret = 0;
	}
	PluginDvbLogcat("PluginAutoGetProcess() - return, s32Ret = %d\r\n", s32Ret);
	
	return s32Ret;	
}

